package com.example.wangxinrong.views;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.read.R;
import com.example.wangxinrong.adapter.XiaoHuaAdapter;
import com.example.wangxinrong.viewmodel.XiaoHuaViewModel;

public class XiaoHuaActivity extends AppCompatActivity {
    private XiaoHuaViewModel viewModel = new XiaoHuaViewModel();

    private XiaoHuaAdapter adapter = new XiaoHuaAdapter();

    private int pagenum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiaohua);

        AppCompatImageView iv_back = findViewById(R.id.iv_back);
        RecyclerView rv_list = findViewById(R.id.rv_list);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rv_list.setLayoutManager(new LinearLayoutManager(this));
        rv_list.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                pagenum ++;
                viewModel.getList(pagenum);
            }
        }, rv_list);
        viewModel.getList(pagenum);
        viewModel.eventList.observe(this, details -> {
            adapter.addData(details.getList());
            if (details.getList().size() >= 20) {
                adapter.loadMoreComplete();
            } else {
                adapter.loadMoreEnd();
            }
        });
    }
}